package com.wh.springbootshiromanyrealm.controller;

import com.wh.springbootshiromanyrealm.pojo.Admin;
import com.wh.springbootshiromanyrealm.pojo.User;
import com.wh.springbootshiromanyrealm.service.IAdminService;
import com.wh.springbootshiromanyrealm.service.IUserService;
import com.wh.springbootshiromanyrealm.token.MyToken;
import com.wh.springbootshiromanyrealm.type.LoginType;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Auther: HuiEr
 * @Date: 2020/3/4 19:08
 * @Description
 */
@Controller
public class MyController {

    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/userLogin")
    public String userLogin(User user){
        MyToken myToken = new MyToken(user.getUsername(), user.getPassword(), LoginType.User.getType());
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(myToken);
            return "UserSuccess";
        }catch (UnknownAccountException e){
            System.out.println("用户名错");
        }catch (IncorrectCredentialsException e){
            System.out.println("密码错");
        }
        return "login";
    }
    @RequestMapping("/adminLogin")
    public String adminLogin(Admin admin){
        MyToken myToken = new MyToken(admin.getUsername(), admin.getPassword(), LoginType.Admin.getType());
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(myToken);
            return "adminSuccess";
        }catch (UnknownAccountException e){
            System.out.println("用户名错");
        }catch (IncorrectCredentialsException e){
            System.out.println("密码错");
        }
        return "login";
    }
}
